using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Perovich.GameObjects.TwoDimensional.Particles
{
    /// <summary>
    /// ExplosionParticleSystem is a specialization of ParticleSystem which creates a
    /// fiery explosion. It should be combined with ExplosionSmokeParticleSystem for
    /// best effect.
    /// </summary>
    public class Explosion : ParticleSystem
    {
        /// <summary>
        /// Constructs a new explosion particle system.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="howManyEffects"></param>
        public Explosion(ICanHoldComponents game, int howManyEffects)
            : base(game, howManyEffects)
        {
            Depth = 0.0f;
        }

        /// <summary>
        /// Set up the constants that will give this particle system its behavior and
        /// properties.
        /// </summary>
        protected override void InitializeConstants()
        {

            // high initial speed with lots of variance.  make the values closer
            // together to have more consistently circular explosions.
            minInitialSpeed = 40;
            maxInitialSpeed = 400;

            // doesn't matter what these values are set to, acceleration is tweaked in
            // the override of InitializeParticle.
            minAcceleration = 0;
            maxAcceleration = 0;

            // explosions should be relatively short lived
            minLifetime = .5f;
            maxLifetime = 1.0f;

            minScale = .3f;
            maxScale = 1.0f;

            // we need to reduce the number of particles on Windows Phone in order to keep
            // a good framerate
            MinimumParticleNumber = 15;
            MaximumParticleNumber = 20;

            minRotationSpeed = -MathHelper.PiOver4;
            maxRotationSpeed = MathHelper.PiOver4;

            // additive blending is very good at creating fiery effects.
            blendState = BlendState.Additive;

            DrawOrder = AdditiveDrawOrder;
        }
        /// <summary>
        /// Initializes a particle in an explosion.
        /// </summary>
        /// <param name="particleIndex"></param>
        /// <param name="where"></param>
        protected override void InitializeParticle(int particleIndex, Vector2 where)
        {

            base.InitializeParticle(particleIndex, where);

            // The base works fine except for acceleration. Explosions move outwards,
            // then slow down and stop because of air resistance. Let's change
            // acceleration so that when the particle is at max lifetime, the velocity
            // will be zero.

            // We'll use the equation vt = v0 + (a0 * t). (If you're not familar with
            // this, it's one of the basic kinematics equations for constant
            // acceleration, and basically says:
            // velocity at time t = initial velocity + acceleration * t)
            // We'll solve the equation for a0, using t = particle.Lifetime and vt = 0.
            Particles[particleIndex] = new Particle(Particles[particleIndex].Position, Particles[particleIndex].Velocity, -Particles[particleIndex].Velocity / Particles[particleIndex].Lifetime, Particles[particleIndex].Lifetime, Particles[particleIndex].Scale, Particles[particleIndex].RotationSpeed, Particles[particleIndex].TimeSinceStart, Particles[particleIndex].Rotation);
        }

        protected override void LoadContent()
        {
            texture = GameObjectsContent.Load<Texture2D>("explosion");
            base.LoadContent();
        }
    }
}
